//
//  UIButton+Button.m
//  time
//
//  Created by zuo on 15/8/20.
//  Copyright (c) 2015年 chenyirong007. All rights reserved.
//

#import "UIButton+XLExtension.h"
#import "UIImage+XLExtension.h"
#import "UIView+XLExtension.h"

@implementation UIButton (XLExtension)


#pragma mark -
#pragma mark - 创建文字背景图片的按钮

+ (UIButton *)addTarget:(id)target
                  title:(NSString *)title
            bgImageName:(NSString *)imgName
        bgImageHighName:(NSString *)imgHighName
    bgImageSelectedName:(NSString *)imgSelectedName
                 action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (imgName) {
        
        [button setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    }
    
    if (imgHighName) {
        
        [button setBackgroundImage:[UIImage imageNamed:imgHighName] forState:UIControlStateHighlighted];
    }
    
    if (imgSelectedName) {
        
        [button setBackgroundImage:[UIImage imageNamed:imgSelectedName] forState:UIControlStateSelected];
    }
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateSelected];
    button.titleLabel.font = xlFont(12);
    [button setTitleColor:xlHexColor(@"#165c4f") forState:UIControlStateNormal];
    [button setTitleColor:xlHexColor(@"#165c4f") forState:UIControlStateHighlighted];
    [button setTitleColor:xlHexColor(@"#165c4f") forState:UIControlStateSelected];

    // 设置按钮的尺寸为背景图片的尺寸
    button.zxl_size = button.currentBackgroundImage.size;
    // 监听按钮点击
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

#pragma mark -
#pragma mark - 创建只有图片的按钮

+ (UIButton *)addTarget:(id)target
              bgImageName:(NSString *)imgName
          bgImageHighName:(NSString *)imgHighName
      bgImageSelectedName:(NSString *)imgSelectedName
                 action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (imgName) {
        
        [button setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    }
    
    if (imgHighName) {
        
        [button setBackgroundImage:[UIImage imageNamed:imgHighName] forState:UIControlStateHighlighted];
    }
    
    if (imgSelectedName) {
        
        [button setBackgroundImage:[UIImage imageNamed:imgSelectedName] forState:UIControlStateSelected];
    }
    
    // 设置按钮的尺寸为背景图片的尺寸
    button.zxl_size = button.currentBackgroundImage.size;
    // 监听按钮点击
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UIButton *)addTarget:(id)target
              imageName:(NSString *)imgName
          imageHighName:(NSString *)imgHighName
      imageSelectedName:(NSString *)imgSelectedName
                 action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (imgName) {
        
        [button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    }
    
    if (imgHighName) {
        
        [button setImage:[UIImage imageNamed:imgHighName] forState:UIControlStateHighlighted];
    }
    
    if (imgSelectedName) {
        
        [button setImage:[UIImage imageNamed:imgSelectedName] forState:UIControlStateSelected];
    }
    
    // 设置按钮的尺寸为背景图片的尺寸
    button.zxl_size = button.currentImage.size;
    // 监听按钮点击
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}



#pragma mark -
#pragma mark - 创建只有文字的按钮
/**
 *  创建一个只有普通状态下文字的按钮
 */
+ (UIButton *)addTarget:(id)target
                  title:(NSString *)title
              textColor:(UIColor *)textColor
              titleFont:(UIFont *)titleFont
                 action:(SEL)action
{
    return [UIButton addTarget:target norTitle:title norColor:textColor selectedTitle:@"" selectedColor:nil titleFont:titleFont backgroundColor:nil action:action];
}
/**
 *  创建一个只有普通状态下文字的按钮
 */
- (UIButton *)initTarget:(id)target
                   title:(NSString *)title
               textColor:(UIColor *)textColor
               titleFont:(UIFont *)titleFont
                  action:(SEL)action
{
    return [UIButton addTarget:target norTitle:title norColor:textColor selectedTitle:@"" selectedColor:nil titleFont:titleFont backgroundColor:nil action:action];
}


/**
 *  创建一个普通状态和被选择状态下只有文字的按钮
 */
+ (UIButton *)addTarget:(id)target
               norTitle:(NSString *)norTitle
               norColor:(UIColor *)norcolor
          selectedTitle:(NSString *)selectedTitle
          selectedColor:(UIColor *)selectedColor
              titleFont:(UIFont *)font
                 action:(SEL)action
{
    return [UIButton addTarget:target norTitle:norTitle norColor:norcolor selectedTitle:selectedTitle selectedColor:selectedColor titleFont:font backgroundColor:[UIColor clearColor] action:action];
}
/**
 *  创建一个普通状态和被选择状态下只有文字的按钮
 */
- (UIButton *)initTarget:(id)target
                norTitle:(NSString *)norTitle
                norColor:(UIColor *)norcolor
           selectedTitle:(NSString *)selectedTitle
           selectedColor:(UIColor *)selectedColor
               titleFont:(UIFont *)font
                  action:(SEL)action
{
    return [UIButton addTarget:target norTitle:norTitle norColor:norcolor selectedTitle:selectedTitle selectedColor:selectedColor titleFont:font backgroundColor:[UIColor clearColor] action:action];
}


/**
 *  创建一个普通和高亮状态下文字的按钮
 */
+ (UIButton *)addTarget:(id)target
                  title:(NSString *)title
              textColor:(UIColor *)textColor
          highTextColor:(UIColor *)highTextColor
              titleFont:(UIFont *)titleFont
                 action:(SEL)action
{
    return [UIButton addTarget:target norTitle:title norColor:textColor highTitle:title highColor:highTextColor titleFont:titleFont backgroundColor:nil action:action];
}
/**
 *  创建一个普通和高亮状态下文字的按钮
 */
- (UIButton *)initTarget:(id)target
                   title:(NSString *)title
               textColor:(UIColor *)textColor
           highTextColor:(UIColor *)highTextColor
               titleFont:(UIFont *)titleFont
                  action:(SEL)action
{
    return [UIButton addTarget:target norTitle:title norColor:textColor highTitle:title highColor:highTextColor titleFont:titleFont backgroundColor:nil action:action];

}


/**
 *  创建一个普通状态和被选择状态下有文字和背景颜色的按钮
 */
+ (UIButton *)addTarget:(id)target
               norTitle:(NSString *)norTitle
               norColor:(UIColor *)norcolor
          selectedTitle:(NSString *)selectedTitle
          selectedColor:(UIColor *)selectedColor
              titleFont:(UIFont *)font
        backgroundColor:(UIColor *)backgroundColor
                 action:(SEL)action
{
    return [UIButton addTarget:target norTitle:norTitle norColor:norcolor selectedTitle:selectedTitle selectedColor:selectedColor highTitle:@"" highColor:nil titleFont:font backgroundColor:backgroundColor action:action];
}
/**
 *  创建一个普通状态和被选择状态下有文字和背景颜色的按钮
 */
- (UIButton *)initTarget:(id)target
                norTitle:(NSString *)norTitle
                norColor:(UIColor *)norcolor
           selectedTitle:(NSString *)selectedTitle
           selectedColor:(UIColor *)selectedColor
               titleFont:(UIFont *)font
         backgroundColor:(UIColor *)backgroundColor
                  action:(SEL)action
{
    return [UIButton addTarget:target norTitle:norTitle norColor:norcolor selectedTitle:selectedTitle selectedColor:selectedColor titleFont:font backgroundColor:backgroundColor action:action];

}


/**
 *  创建一个普通状态和高亮状态下有文字和背景颜色的按钮
 */
+ (UIButton *)addTarget:(id)target
               norTitle:(NSString *)norTitle
               norColor:(UIColor *)norcolor
          highTitle:(NSString *)highTitle
          highColor:(UIColor *)highColor
              titleFont:(UIFont *)font
        backgroundColor:(UIColor *)backgroundColor
                 action:(SEL)action
{
    return [UIButton addTarget:target norTitle:norTitle norColor:norcolor selectedTitle:@"" selectedColor:nil highTitle:highTitle highColor:highColor titleFont:font backgroundColor:backgroundColor action:action];
}
/**
 *  创建一个普通状态和高亮状态下有文字和背景颜色的按钮
 */
- (UIButton *)initTarget:(id)target
                norTitle:(NSString *)norTitle
                norColor:(UIColor *)norcolor
               highTitle:(NSString *)highTitle
               highColor:(UIColor *)highColor
               titleFont:(UIFont *)font
         backgroundColor:(UIColor *)backgroundColor
                  action:(SEL)action
{
    return [UIButton addTarget:target norTitle:norTitle norColor:norcolor selectedTitle:@"" selectedColor:nil highTitle:highTitle highColor:highColor titleFont:font backgroundColor:backgroundColor action:action];
    
}

/**
 *  创建一个有背景色 高亮 被选择 普通状态下只有文字的按钮
 */

+ (UIButton *)addTarget:(id)target
               norTitle:(NSString *)norTitle
               norColor:(UIColor *)norcolor
          selectedTitle:(NSString *)selectedTitle
          selectedColor:(UIColor *)selectedColor
              highTitle:(NSString *)highTitle
              highColor:(UIColor *)highColor
              titleFont:(UIFont *)font
        backgroundColor:(UIColor *)backgroundColor
                 action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:16]];
    CGSize size =[norTitle sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
    //! 这里需要根据内容大小来调整宽度
    button.bounds = CGRectMake(0, 0, size.width <= 10 ? 70 : size.width + 30, size.height+20);
    //button.titleLabel.textColor = [UIColor whiteColor];
    button.titleLabel.font = font;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitle:norTitle forState:UIControlStateNormal];
    [button setTitleColor:norcolor forState:UIControlStateNormal];
    if (selectedColor&&selectedTitle.length) {
        [button setTitle:selectedTitle forState:UIControlStateSelected];
        [button setTitleColor:selectedColor forState:UIControlStateSelected];
    }
    if (highColor&&highTitle.length) {
        [button setTitle:highTitle forState:UIControlStateHighlighted];
        [button setTitleColor:highColor forState:UIControlStateHighlighted];
    }
    if (backgroundColor) {
        [button setBackgroundColor:backgroundColor];
    }
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;

}
#pragma mark -
#pragma mark - 创建高亮纯背景颜色图片的按钮
+ (UIButton *)addTarget:(id)target
               norTitle:(NSString *)norTitle
               norColor:(UIColor *)norcolor
          selectedTitle:(NSString *)selectedTitle
          selectedColor:(UIColor *)selectedColor
              highTitle:(NSString *)highTitle
              highColor:(UIColor *)highColor
              titleFont:(UIFont *)font
          norBgColorImg:(UIColor *)norBgColorImg
         highBgColorImg:(UIColor *)highBgColorImg
     selectedBgColorImg:(UIColor *)selectedBgColorImg
                 action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:16]];
    CGSize size =[norTitle sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
    //! 这里需要根据内容大小来调整宽度
    button.bounds = CGRectMake(0, 0, size.width <= 10 ? 70 : size.width + 30, size.height+20);
    //button.titleLabel.textColor = [UIColor whiteColor];
    button.titleLabel.font = font;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitle:norTitle forState:UIControlStateNormal];
    [button setTitleColor:norcolor forState:UIControlStateNormal];
    if (selectedColor&&selectedTitle.length>0) {
        [button setTitle:selectedTitle forState:UIControlStateSelected];
        [button setTitleColor:selectedColor forState:UIControlStateSelected];
    }
    if (highColor&&highTitle.length>0) {
        [button setTitle:highTitle forState:UIControlStateHighlighted];
        [button setTitleColor:highColor forState:UIControlStateHighlighted];
    }
    if (norBgColorImg) {
        [button setBackgroundImage:[UIImage imageWithColor:norBgColorImg] forState:UIControlStateNormal];
    }
    if (highBgColorImg) {
        [button setBackgroundImage:[UIImage imageWithColor:highBgColorImg] forState:UIControlStateHighlighted];
    }
    if (selectedBgColorImg) {
        [button setBackgroundImage:[UIImage imageWithColor:selectedBgColorImg] forState:UIControlStateHighlighted];

    }
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
@end
